package com.hxkj.file.service.impl;

import com.hxkj.file.entity.enums.FilePathEnum;
import com.hxkj.file.service.FileService;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.storage.service.FileComponent;
import com.platform.common.storage.utils.file.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;


/**
 * 文件上传
 * <p>
 * Created by chenwei on 2018/12/11.
 */
@Service("fileService")
@Slf4j
public class FlieServiceImpl implements FileService {

    /**
     * 文件图片类型
     */
    private static final String[] ALLOW_IMAGE_FILE_TYPES = {"gif", "jpg", "jpeg", "bmp", "png", "psd"};

    @Resource
    private FileComponent fileComponent;

    @Value("${storage.imgUrl}")
    private String imgUrl;

    @Override
    public String imgUpload(MultipartFile files) throws Exception {
        String fileType = FileUtil.getFileType(files.getInputStream());
        if (!ArrayUtils.contains(ALLOW_IMAGE_FILE_TYPES, fileType)) {
            throw new CustomMessageException("请选择合理的图片类型,仅支持:gif,jpg,jpeg,png格式的图片文件");
        }
        String src = fileComponent.uploadImage(files, FilePathEnum.IMG_FILE_PATH.getDescription(), 100, 240, 300, true, 0.6f);
        return src;
    }
}
